package com.yuexunit.mobilenurse.module.BloodManage.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.yuexunit.mobilenurse.R;
import com.yuexunit.mobilenurse.base.titlebar.TitleBar_DocAdvice;
import com.yuexunit.mobilenurse.config.AppConfig;
import com.yuexunit.mobilenurse.module.BloodManage.adapter.BloodBagAdapter;
import com.yuexunit.mobilenurse.module.BloodManage.bean.BloodBag;
import com.yuexunit.mobilenurse.module.BloodManage.bean.BloodBagDataBean;
import com.yuexunit.mobilenurse.module.BloodManage.model.impl.BloodBagApplyModel;
import com.yuexunit.mobilenurse.module.BloodManage.presenter.IBloodBagApplyPresenter;
import com.yuexunit.mobilenurse.module.BloodManage.presenter.impl.BloodBagApplyPresenter;
import com.yuexunit.mobilenurse.module.BloodManage.ui.view.IBloodBagApplyView;
import com.yuexunit.mobilenurse.util.CreateJson;
import com.yuexunit.mobilenurse.widget.EmptyLayout;

import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;
import org.kymjs.kjframe.utils.SystemTool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by work-jx on 2017/10/12.
 */
public class Act_Blood_ApplyDetail extends TitleBar_DocAdvice implements IBloodBagApplyView{
    @Bind(R.id.titlebar_docadvice_tv_title)
    public TextView titlebar_docadvice_tv_title;
    @Bind(R.id.docactvice_name)
    public TextView docactvice_name;
    @Bind(R.id.docactvice_bedno)
    public TextView docactvice_bedno;
    @Bind(R.id.docactvice_visitno)
    public TextView docactvice_visitno;
    @Bind(R.id.act_testdetail_list)
    public ListView act_testdetail_list;
    @Bind(R.id.empty_layout)
    public EmptyLayout mEmptyLayout;
    @Bind(R.id.act_blood_applydetail_no_tv)
    TextView actBloodApplydetailNoTv;

    public static final int DOWNLOAD_NETERROR = -1;

    //记录Log
    private final Logger log = LoggerFactory.getLogger(Act_Blood_ApplyDetail.class);

    BaseAdapter adapter;

    IBloodBagApplyPresenter presenter;

    String requireNo ;

    ArrayList<BloodBag> bloodBags = new ArrayList<>();

    @Override
    public void setRootView() {
        setContentView(R.layout.act_blood_applydetail);
        ButterKnife.bind(this);
        presenter = new BloodBagApplyPresenter(new BloodBagApplyModel(),this);
        requireNo = getIntent().getExtras().getString("requireNo");
    }

    @Override
    public void initData() {
        super.initData();
        actBloodApplydetailNoTv.setText(requireNo);
        mEmptyLayout.setOnLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mEmptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
            }
        });

        act_testdetail_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle bundle = new Bundle();
                bundle.putString("requireNo",requireNo);
                bundle.putSerializable("bloodbag",bloodBags.get(position));
                Intent intent = new Intent(aty,Act_Blood_BloodDetail.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        if(SystemTool.checkNet(aty)) {
            Map<String, String> params = new HashMap<>();
            params.put("requireNo",requireNo);
            presenter.getBloodBagDataList(params);
        }
        else
        {
            ViewInject.toast("网络异常，请检查网络是否连接!");
            log.error(CreateJson.LOG_JSON("", "1", PreferenceHelper.readString(aty, AppConfig.NURSE_INFO, AppConfig.NURSE_DEPTNO), ""));
        }
    }

    @Override
    public void setTitle()
    {
        titlebar_docadvice_tv_title.setText("血袋列表");
        docactvice_name.setText(PreferenceHelper.readString(aty, AppConfig.CURRENT_PATIENT_INFO, AppConfig.CURRENT_PATIENT_NAME));
        docactvice_bedno.setText(PreferenceHelper.readString(aty, AppConfig.CURRENT_PATIENT_INFO, AppConfig.CURRENT_PATIENT_BEDCODE));
        docactvice_visitno.setText(PreferenceHelper.readString(aty, AppConfig.CURRENT_PATIENT_INFO, AppConfig.CURRENT_PATIENT_VISITNO));
    }

    @Override
    public  void onBackClick()
    {
        finish();
    }

    @Override
    public void showBloodBagDataList(BloodBagDataBean bloodBagDataBean) {
        if(bloodBags.size()>0)
        {
            bloodBags.clear();
        }
        bloodBags = bloodBagDataBean.getDatas().get(0).getBagList();
        adapter = new BloodBagAdapter(act_testdetail_list,bloodBags,R.layout.item_blood_bag);
        act_testdetail_list.setAdapter(adapter);
        mEmptyLayout.dismiss();
    }

    @Override
    public void PullDownRefreshState(int state) {
        switch (state) {
            case DOWNLOAD_NETERROR:
                mEmptyLayout.setErrorType(EmptyLayout.NETWORK_ERROR);
                break;
        }
    }
}
